home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1126 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: peng@dpg.rnb.com (Wei Peng)
  3. Newsgroups: comp.std.c++
  4. Subject: Temporary objects in Std
  5. Date: 17 Apr 1996 14:57:29 GMT
  6. Organization: Sun Microsystems Inc., Mountain View, CA
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4107cc$101229.223@news>
  9. NNTP-Posting-Host: taumet.eng.sun.com
  10. X-Newsreader: knews 0.9.6
  11. Originator: clamage@taumet
  12.  
  13. Hi, I'm sure about what the Std says about the lifetime of temp objects (it
  14. says tmp objects may live longer... or something alike).
  15.  
  16. A sample code. Suppose we have a string object called String, all necessary
  17. constructors/operators are well defined. Now we have three functions
  18.  
  19. void doSomething(const char*);
  20.  
  21. String getString();
  22.  
  23. const char* getCharStar()
  24. {
  25.   return returnString();
  26. }
  27.  
  28. Now if we do:
  29.  
  30.           doSomething(getCharStar());
  31.  
  32. Obvously this code doesn't work under old C++, where temp objects only
  33. live within their scope. My impression is that under STD C++ the tmp
  34. object created by getString() should live long enough so doSomething()
  35. can safely execute.
  36.  
  37. But apparently some of the compilers I've tried still execute String
  38. destructor when getCharStar() returns.
  39.  
  40. Wei Peng
  41.  
  42.  
  43. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  44. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  45. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  46. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  47. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  48.